package com.hssoftvn.android.apps.tinycouplechat;

import java.security.PublicKey;

import org.apache.http.HttpConnection;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.os.AsyncTask;


/*AsyncTask<ChatItem, Void, Integer>
 * 	ChatItem: data we want to handle, Parameter for method execute(...) 
 * 	Void : don't care, less using it, let it Void
 * 	Integer: the return values after task is done:
 * 		- Successful or Failed
 * 		- Any ENUM value to be a return value
 * 		- Useful to display some popup or error dialog to user
 */

/* SERVER INFORMATION
 * http://members.000webhost.com/index.php?
 * Username: huynhhoangson@gmail.com
 * Password: ...told..02
 * Server URL: http://tinycouplechat.hssoftvn.comlu.com/
 */
public class ChatItemListener extends AsyncTask<Void, ChatItem, ChatItem>
{
    ProgressDialog pdLoading = new ProgressDialog(TinyCoupleChat.getInstance());

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    
    /*
     *  RETURN VALUES:
     * 		SUCCESSFUL: sent ok
     *		NETWORK_ERROR: network error
     */
    
    @Override
    protected ChatItem doInBackground(Void... params) {

        // this method will be running on background thread so don't update UI frome here
        // do your long running http tasks here, 
    	// you dont want to pass argument and u can access the parent class' variable url over here
    	/*
    	 * HANDLING MESSAGE STATUS HERE
    	 */
    	
    	ChatItem item = new ChatItem("", "", Utils.TYPE_YOU);
    	String requests = "";
    	String responses = "";
    	item.setStatus(Utils.STATUS_UNKNOWN);

    	try{
    		
    		//Requests
    		requests = NetworkHelper.URL_CHECKING_INCOMING_MSG;
    		
    		//Responses
    		responses = NetworkHelper.getHttpResponse(requests);
    		//if ( !NetworkHelper.doesResponseHasError(responses) )
    		{
    			//e|errorCode|f|FROM|m|MESSAGE|d|DATE SENT
    			String[] temp = responses.replace('|', '~').split("~");
    			String errorCode=temp[1];
    			String from=temp[3];
    			String message=temp[5];
    			String datesent=temp[7];
    			item.setText(message);    			
    		}
    		//Update sending status
    	} catch(Exception ex){    		
    	}
    	
        return item;
    }
    
    @Override
    protected void onPostExecute(ChatItem result) {
    	// TODO Auto-generated method stub
    	super.onPostExecute(result);
    	Utils.getApplication().addCouplexToChatWindow(result);
    }
       
}
